package com.aaa.controller.api;

import com.aaa.entity.Dept;
import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.aaa.vo.UserVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("system/api/user")

public class UserControllerApi {
    @Autowired
    private UserService userService;
    @GetMapping("getInfo")
    public Result<User> getInfo(){
        return userService.getUserInfo();
    }

    @PostMapping("searchUser/{currentPage}/{pageSize}")
    public Result<IPage<User>> searchUser(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody UserVo userVo){
        return userService.findByConditions(currentPage, pageSize, userVo);
    }
//    //添加
//    @PostMapping("addUser")
//    public Result addUser(User user){
//        boolean b = userService.saveOrUpdate(user);
//        return new Result(b);
//    }
    //添加修改
    @PostMapping("saveAndUpdate")
    public Result saveAndUpdate(User user){
        if (userService.saveAndUpdate(user)){
            return  new Result(200,"操作成功",true);
        }else {
            return new Result(500,"操作失败",false);
        }
    }

    //删除
    @DeleteMapping("delById/{id}")
    public Result delById(@PathVariable Long id){
        if (userService.delById(id)){
           return new Result(200,"修改成功",true);
        }else {
            return  new Result(500,"修改失败",false);
        }
    }
    //批量删除
    @PostMapping("delListById")
    public Result delListByid(@RequestBody List<User> userList){
        userList.forEach((User user)->{
            userService.removeById(user.getUserId());
        });
        return new Result(200,"批量删除成功",true);
    }

    @PostMapping("/loginOut")
    public Result<String> loginOut(){
        return userService.loginOut();
    }


}
